#include <QCoreApplication>
#include <EasyMsg.h>
#include "instancetestlib.h"
#include "../MsgDef/msgdef.h"   //消息定义在此处查看
#include <QDebug>
using namespace EM;

void g_func(EasyMsg* msg){
    qDebug() << Q_FUNC_INFO <<msg->id();
}

//测试类
class Test{
public:
    Test(){
       subscript<AMsg>(&Test::dosomething, this);//订阅AMsg 收到消息时执行回调函数
       subscript<BMsg>(&g_func);
       subscript<BMsg>(&Test::dosomething, this);//订阅BMsg 收到消息时执行回调函数
       //subscript<int>(&Test::dosomething,this);//无效的消息 运行将会触发断言
    }
    //回调函数的参数必须为 EasyMsg*
    void dosomething(EasyMsg* msg)
    {
        qDebug() << Q_FUNC_INFO <<msg->id();
        if(msg->id() == MsgId<BMsg>())//MsgId<BMsg>()可通过消息类型获取消息id
        {
            qDebug() << "BMsg::a = " <<static_cast<BMsg*>(msg)->a;
        }
    }
};

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    InstanceTestLib it; // subcript CMsg
    Test t;// subscript AMsg

    //含有成员变量的消息
    BMsg * bmsg = new BMsg();
    bmsg->a = 1;
    pushMsg(bmsg);

    //模块间消息发送 请看InstanceTestLib::doSomething
    pushMsg(new CMsg());

    return a.exec();
}
